﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MSI = Microsoft.Deployment.WindowsInstaller;
using IO = System.IO;
using SLP;

namespace MsiCustomize
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        MstProject MSTProject;

        public MainWindow()
        {
            InitializeComponent();
            MSTProject = new MstProject();
            this.DataContextChanged += MainWindow_DataContextChanged;
        }

        private void MainWindow_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            ProjectTabComtrols.IsEnabled = this.DataContext != null;
        }

        private void FileNew_Exec(object sender, ExecutedRoutedEventArgs e)
        {
            // TODO - Check to see if there is a current project and if it needs to be svaed
            MSTProject = new MstProject();
            MSTProject.FeatureName = Environment.UserDomainName + " Customizations";
            this.DataContext = MSTProject;
        }

        private void FileOpen_Exec(object sender, ExecutedRoutedEventArgs e)
        {
            // TODO - Check to see if there is a current project and if it needs to be svaed
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Title = "New MSI Customization Project";
            dlg.Filter = "MSI Customization Project Files|*.msicp|All Files|*.*";
            dlg.FilterIndex = 0;
            if (dlg.ShowDialog(this) == true)
            {
                MSTProject = MstProject.Load(dlg.FileName);
                this.DataContext = MSTProject;
            }
        }

        private void FileSave_Exec(object sender, ExecutedRoutedEventArgs e)
        {
            if (MSTProject.HasPendingChanges)
            {
                if (String.IsNullOrEmpty(MSTProject.FileName))
                    FileSaveAs_Exec(sender, e);
                else
                    MSTProject.Save();
            }
        }

        private void FileSaveAs_Exec(object sender, ExecutedRoutedEventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Title = "New MSI Customization Project";
            dlg.Filter = "MSI Customization Project Files|*.msicp|All Files|*.*";
            dlg.FilterIndex = 0;
            if (dlg.ShowDialog(this) == true)
            {
                MSTProject.Save(dlg.FileName);
            }
        }

        private void FileExit_Exec(object sender, ExecutedRoutedEventArgs e)
        {
            // TODO - Check to see if there is a current project and if it needs to be svaed
            Application.Current.Shutdown(0);
        }

        private void HelpAbout_Exec(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show(this,"Help About");
        }

    }
}
